package com.hailiao.beidou.message.ui.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import com.d10ng.compose.ui.AppText
import com.hailiao.beidou.message.R
import com.hailiao.beidou.message.bd.Beidou
import com.hailiao.beidou.message.bd.constant.BDStatus
import com.hailiao.beidou.message.data.setting.SettingRepository
import com.hailiao.beidou.message.data.store.contant.CommunicationMode
import com.hailiao.beidou.message.ui.page.destinations.BLEConnectScreenDestination
import com.hailiao.beidou.message.ui.page.destinations.SPConnectScreenDestination
import com.hailiao.beidou.message.ui.page.destinations.StatusScreenDestination
import com.hailiao.beidou.message.utils.go

/**
 * 状态栏
 * @Author d10ng
 * @Date 2024/3/2 13:47
 */

@Composable
fun BDStatusBar() {
    val status by Beidou.statusFlow.collectAsState()
    val waitTime by Beidou.waitTimeFlow.collectAsState()
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .background(status.color)
            .clickable {
                if (status != BDStatus.DISCONNECT) {
                    go(StatusScreenDestination.route)
                } else if (SettingRepository.commModeFlow.value == CommunicationMode.BLE) {
                    go(BLEConnectScreenDestination.route)
                } else {
                    go(SPConnectScreenDestination.route)
                }
            }
            .padding(horizontal = 19.dp, vertical = 9.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        val rotating = remember(status) { status == BDStatus.CONNECTING }
        RotatingImage(
            painter = painterResource(id = status.iconID),
            modifier = Modifier.size(20.dp),
            rotating = rotating
        )
        Text(
            text = status.tips,
            style = AppText.Normal.Body.small,
            modifier = Modifier.padding(start = 12.dp),
            maxLines = 1,
            overflow = TextOverflow.Ellipsis,
        )
        Spacer(modifier = Modifier.weight(1f))
        if (status == BDStatus.DISCONNECT) {
            Text(
                text = "选择设备连接",
                style = AppText.Normal.Primary.small,
                color = Color(0xFFFC4107),
                modifier = Modifier
                    .padding(end = 4.dp),
                maxLines = 1,
                overflow = TextOverflow.Ellipsis,
            )
            Icon(
                painter = painterResource(id = R.drawable.bd_status_connect_arrow),
                contentDescription = null,
                modifier = Modifier.size(18.dp),
                tint = Color(0xFFFC4107)
            )
        } else if (waitTime > 0) {
            Text(text = "(${waitTime})", style = AppText.Normal.Body.small)
        }
    }
}